<template>
  <PageWrapper dense contentFullHeight fixedHeight contentClass="flex">
    <BasicTable @register="registerTable" :searchInfo="searchInfo">
      <template #toolbar>
        <a-button type="primary" @click="handleCreate">{{ t('sys.code.addTitle') }}</a-button>
      </template>
    </BasicTable>
    <AccountModal @register="registerModal" @success="handleSuccess" />
  </PageWrapper>
</template>
<script lang="ts" setup>
  import { reactive, ref, computed } from 'vue';
  import { useModal } from '@/components/Modal';
  import { message } from 'ant-design-vue';
  import { BasicTable, useTable, TableAction } from '@/components/Table';
  import { getcodeAPI } from '@/api/demo/code';
  import { PageWrapper } from '@/components/Page';
  import { columns } from './account.data';
  import { useGo } from '@/hooks/web/usePage';
  import AccountModal from './AccountModal.vue';
  import { useI18n } from "@/hooks/web/useI18n";

  const { t } = useI18n();
  defineOptions({ name: 'AccountManagement' });
  const total = ref(0);

  const tableTitle = computed(() => `${t('sys.code.title')} ${t('sys.configuration.common')} ${total.value} ${t('sys.configuration.strip')}`);
  const go = useGo();
  const searchInfo = reactive<Recordable>({});
  const [registerModal, { openModal }] = useModal();
  const [registerTable, { reload,updateTableDataRecord }] = useTable({
    title: tableTitle,
    api: async (params) => {
      const result = (await getcodeAPI(params)) as { items: any[]; total: number };
      total.value = result.total;
      return result;
    },
    rowKey: 'id',
    columns,

    useSearchForm: false,
    showTableSetting: true,
    bordered: true,
    // actionColumn: {
    //   width: 120,
    //   title: t('sys.configuration.action'),
    //   dataIndex: 'action',
    // },
  });

  async function handleDelete(record: Recordable) {
    await dataDeleteAPI({ id: record.id });
    message.success('操作完成');
    reload();
  }

  function handleView(record: Recordable) {
    go('/system/account_detail/' + record.id);
  }

  function handleCreate() {
    openModal(true, {
      isUpdate: false,
    });
  }

  function handleSuccess({ isUpdate, values }) {
    if (isUpdate) {
      const result = updateTableDataRecord(values.id, values);
    } else {
      reload();
    }
  }
</script>
